package com.bac.rentmap.web.controller.ad.rating;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.FlatAdDao;
import com.bac.rentmap.ad.flat.rating.FlatAdRatingCalculator;
import com.bac.rentmap.ad.flat.rating.NormalizedRatingExplanation;

public class NormalizedRatingExplainingController extends AbstractController {
    private FlatAdRatingCalculator flatAdRatingCalculator;
    private FlatAdDao flatAdDao;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Long flatAdId = Long.valueOf(request.getParameter("flatAdId"));
        FlatAd flatAd = flatAdDao.get(flatAdId);
        ModelAndView res = new ModelAndView("ad/search/ratingExplanation");
        res.addObject("flatAd", flatAd);
        if(flatAd != null){
            NormalizedRatingExplanation ratingExplanation = flatAdRatingCalculator
                .explainNormalizedRating(flatAd);

            res.addObject("ratingExplanation", ratingExplanation);
        }
        return res;
    }

    @Required
    public void setFlatAdRatingCalculator(
            FlatAdRatingCalculator flatAdRatingCalculator) {
        this.flatAdRatingCalculator = flatAdRatingCalculator;
    }

    @Required
    public void setFlatAdDao(FlatAdDao flatAdDao) {
        this.flatAdDao = flatAdDao;
    }
}
